<template>
  <div class="profile">
    <h2>个人中心</h2>
    <el-row :gutter="20">
      <el-col :span="8">
        <el-card>
          <div class="user-avatar">
            <el-avatar :size="100" :src="user.avatar" />
          </div>
          <h3 style="text-align: center;">{{ user.username }}</h3>
          <p style="text-align: center; color: #666;">{{ user.email }}</p>
        </el-card>
      </el-col>
      <el-col :span="16">
        <el-card>
          <el-tabs v-model="activeTab">
            <el-tab-pane label="个人信息" name="info">
              <el-form :model="user" label-width="80px">
                <el-form-item label="用户名">
                  <el-input v-model="user.username" />
                </el-form-item>
                <el-form-item label="邮箱">
                  <el-input v-model="user.email" />
                </el-form-item>
                <el-form-item label="手机">
                  <el-input v-model="user.phone" />
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="updateProfile">保存</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
            <el-tab-pane label="修改密码" name="password">
              <el-form :model="passwordForm" label-width="80px">
                <el-form-item label="原密码">
                  <el-input v-model="passwordForm.oldPassword" type="password" />
                </el-form-item>
                <el-form-item label="新密码">
                  <el-input v-model="passwordForm.newPassword" type="password" />
                </el-form-item>
                <el-form-item label="确认密码">
                  <el-input v-model="passwordForm.confirmPassword" type="password" />
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="changePassword">修改密码</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useAppStore } from '../store'
import { ElMessage } from 'element-plus'

export default {
  name: 'Profile',
  setup() {
    const store = useAppStore()
    
    const activeTab = ref('info')
    const user = ref({
      username: '用户',
      email: 'user@example.com',
      phone: '13800138000',
      avatar: ''
    })

    const passwordForm = ref({
      oldPassword: '',
      newPassword: '',
      confirmPassword: ''
    })

    onMounted(() => {
      if (store.user) {
        user.value = { ...user.value, ...store.user }
      }
    })

    const updateProfile = () => {
      ElMessage.success('个人信息更新成功！')
    }

    const changePassword = () => {
      if (passwordForm.value.newPassword !== passwordForm.value.confirmPassword) {
        ElMessage.error('两次输入的密码不一致')
        return
      }
      ElMessage.success('密码修改成功！')
      passwordForm.value = {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      }
    }

    return {
      activeTab,
      user,
      passwordForm,
      updateProfile,
      changePassword
    }
  }
}
</script>

<style scoped>
.user-avatar {
  text-align: center;
  margin-bottom: 20px;
}
</style>